package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.AdminRole;
import com.rongke.service.AdminRoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @AdminRoleController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/adminRole")
@Transactional
@CrossOrigin
public class AdminRoleController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * @添加
     * @param adminRole
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addAdminRole(@RequestBody AdminRole adminRole){
        log.debug("添加");
        adminRole.setAvailable("1");
        adminRoleService.insert(adminRole);
        return JsonResp.ok(adminRole);
    }

    /**
     * @修改
     * @param adminRole
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateAdminRole(@RequestBody AdminRole adminRole){
        log.debug("修改");
        adminRoleService.updateById(adminRole);
        return JsonResp.ok(adminRole);
    }
    /**
     * @根据id查找
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/findRoleList", method = RequestMethod.GET)
    public JsonResp findRoleList(){
        log.debug("查找");
        EntityWrapper<AdminRole> ew=new EntityWrapper<>();
        ew.eq("available",1);
        List<AdminRole> adminRoles = adminRoleService.selectList(ew);
        return JsonResp.ok(adminRoles);
    }

    /**
     * @根据条件查找
     * @param adminRole
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectAdminRoleByCondition(@RequestBody AdminRole adminRole){
        log.debug("根据条件查找");
        if(adminRole==null)
            return JsonResp.toFail("参数不能为空");
        List<AdminRole> adminRoleList=adminRoleService.selectByCondition(adminRole);
        return JsonResp.ok(adminRoleList);
    }

}
